
global fmtDEC $fmtDEC
global fmtDEC bdec(4) sdec(4)

cap program drop outFlincom1st
program def outFlincom1st
* syntax anything
qui lincom $Xsum
local pvalFsum=2*ttail($addClust,abs(r(estimate)/r(se)))
local Fsum=r(estimate)
qui test $X
local Fjoint=r(p)
qui su `e(depvar)' if e(sample)
local Yavg=r(mean)
if (`Fjoint'<.) outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC addstat(pct impact, `=`Fsum'/`Yavg'', sum,`Fsum',F test sum=0, `pvalFsum', F test joint significance, `Fjoint', Mean Dep Var, `Yavg') append
if (`Fjoint'>=.) outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC append
end

cap program drop outFlincom1strain
program def outFlincom1strain
	* syntax anything
	qui lincom $Xsum
	local pvalFsum=2*ttail($addClust,abs(r(estimate)/r(se)))
	local Fsum=r(estimate)
	qui lincom $XsumR
	local pvalFsumR=2*ttail($addClust,abs(r(estimate)/r(se)))
	local FsumR=r(estimate)
	qui test $X
	local Fjoint=r(p)
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	if (`Fjoint'<.) outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC addstat(pct impact P, `=`Fsum'/`Yavg'', sum P,`Fsum',F test sum P=0, `pvalFsum', pct impact RP, `=`FsumR'/`Yavg'', sum R,`FsumR',F test sum R=0, `pvalFsumR', Mean Dep Var, `Yavg') append
	if (`Fjoint'>=.) outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC append
end

cap program drop outFlincomON
program def outFlincomON
	* syntax anything
	qui lincom $Xsum
	local pvalFsum=2*ttail($addClust,abs(r(estimate)/r(se)))
	local Fsum=r(estimate)
	matrix pvalON_$est = (nullmat(pvalON_$est ) \ `pvalFsum')
end

cap program drop outFlincomEND
program def outFlincomEND
	* syntax anything
	qui lincom $Xsum
	local pvalFsum=2*ttail($addClust,abs(r(estimate)/r(se)))
	local Fsum=r(estimate)
	matrix pvalEND_$est = (nullmat(pvalEND_$est ) \ `pvalFsum')
end

cap program drop MSSoutFlincomON
program def MSSoutFlincomON
	* syntax anything
	qui lincom $Xsum
	local pvalFsum=2*ttail($addClust,abs(r(estimate)/r(se)))
	local Fsum=r(estimate)
	matrix pvalON = (nullmat(pvalON ) \ `pvalFsum')
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	matrix impactON = (nullmat(impactON ) \ `=`Fsum'/`Yavg'')
end

cap program drop MSSoutFlincomEND
program def MSSoutFlincomEND
	* syntax anything
	qui lincom $Xsum
	local pvalFsum=2*ttail($addClust,abs(r(estimate)/r(se)))
	local Fsum=r(estimate)
	matrix pvalEND = (nullmat(pvalEND ) \ `pvalFsum')
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	matrix impactEND = (nullmat(impactEND ) \ `=`Fsum'/`Yavg'')
end

cap program drop outFlincom
program def outFlincom
	* syntax anything
	qui lincom $Xsum
	local pvalFsum=2*ttail($addClust,abs(r(estimate)/r(se)))
	local Fsum=r(estimate)
	matrix pvalAGGSUM = (nullmat(pvalAGGSUM) \ `pvalFsum')
	qui test $X
	local Fjoint=r(p)
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	if (`Fjoint'<.) outreg2 using $table, asterisk(se) $addClust drop($year $country_year Iccid* It*)  excel $fmtDEC addstat(pct impact, `=`Fsum'/`Yavg'', sum,`Fsum',F test sum=0, `pvalFsum', F test joint significance, `Fjoint', Mean Dep Var, `Yavg') append
	if (`Fjoint'>=.) outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC append
end

cap program drop outFlincomIMPORT
program def outFlincomIMPORT
	* syntax anything
	qui lincom $Xsum
	local pvalFsum=2*ttail($addClust,abs(r(estimate)/r(se)))
	local Fsum=r(estimate)
	matrix pvalAGGSUM = (nullmat(pvalAGGSUM) \ `pvalFsum')
	qui test $X
	local Fjoint=r(p)
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	qui testparm $IMPX
	local pvalIMP=r(p)
	if (`Fjoint'<.) outreg2 using $table, asterisk(se) $addClust drop($year $country_year Iccid* It*)  excel $fmtDEC addstat(pct impact, `=`Fsum'/`Yavg'', sum,`Fsum',F test sum=0, `pvalFsum', F test joint significance, `Fjoint', Mean Dep Var, `Yavg', F test import pval, `pvalIMP') append
	if (`Fjoint'>=.) outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC append
end


cap program drop outFlincom_IV
program def outFlincom_IV
	* syntax anything
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	local Ffirst = e(widstat)
	local IDp = e(idp)
	
	* This adds these stats to outreg only if they are produced to avoid problems due to the covariance matrix not having full rank
	local Ffirst_string ""
	if "`Ffirst'" != "." {
		local Ffirst_string ", First Stage F Stat, `Ffirst'"
	}
	local IDp_string "" 
	if "`IDp'" != "." {
		local IDp_string ", Underidentification test pval, `IDp'"
	}
	
	* Notice that this has optionally added statistics (see above)
	outreg2 using $table, asterisk(se) $addClust drop($year $country_year Iccid* It*)  excel $fmtDEC addstat(Mean Dep Var, `Yavg' `Ffirst_string' `IDp_string') append
end


cap program drop outFdisaglincom1st
program def outFdisaglincom1st
	* syntax anything
	cap qui lincom $annaggsum
	local FsumA=r(estimate)
	local pvalFsumA=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $peraggsum
	local FsumP=r(estimate)
	local pvalFsumP=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $extaggsum
	local FsumE=r(estimate)
	local pvalFsumE=2*ttail($addClust,abs(r(estimate)/r(se)))
	matrix pvalANNUAL = (nullmat(pvalANNUAL) \ `pvalFsumA')
	matrix pvalPERENNIAL = (nullmat(pvalPERENNIAL) \ `pvalFsumP')
	matrix pvalEXTRACT = (nullmat(pvalEXTRACT) \ `pvalFsumE')
	cap qui test $annagg
	local FjointA=r(p)
	cap qui test $peragg
	local FjointP=r(p)
	cap qui test $extagg
	local FjointE=r(p)
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	if (`FsumA'>=.) {
		outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC addstat( Mean Dep Var, `Yavg') append
	}
	else{
		outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC addstat(+A, `FsumA', p, `pvalFsumA', +P, `FsumP', p, `pvalFsumP', +E, `FsumE', p, `pvalFsumE', avgY, `Yavg') append
	}
end



cap program drop outFdisaglincomON
program def outFdisaglincomON
	* syntax anything
	cap qui lincom $annaggsum
	local FsumA=r(estimate)
	local pvalFsumA=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $peraggsum
	local FsumP=r(estimate)
	local pvalFsumP=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $extaggsum
	local FsumE=r(estimate)
	local pvalFsumE=2*ttail($addClust,abs(r(estimate)/r(se)))
	matrix pvalANNUAL_ON$est = (nullmat(pvalANNUAL_ON$est ) \ `pvalFsumA')
	matrix pvalPERENNIAL_ON$est = (nullmat(pvalPERENNIAL_ON$est ) \ `pvalFsumP')
	matrix pvalEXTRACT_ON$est = (nullmat(pvalEXTRACT_ON$est ) \ `pvalFsumE')
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	if "$est"=="_LOG" {
		matrix impactANNUAL_ON$est = (nullmat(impactANNUAL_ON$est ) \ `FsumA')
		matrix impactPERENNIAL_ON$est = (nullmat(impactPERENNIAL_ON$est ) \ `FsumP')
		matrix impactEXTRACT_ON$est = (nullmat(impactEXTRACT_ON$est ) \ `FsumE')
	}
	else {
		matrix impactANNUAL_ON$est = (nullmat(impactANNUAL_ON$est ) \ `=`FsumA'/`Yavg'')
		matrix impactPERENNIAL_ON$est = (nullmat(impactPERENNIAL_ON$est ) \ `=`FsumP'/`Yavg'')
		matrix impactEXTRACT_ON$est = (nullmat(impactEXTRACT_ON$est ) \ `=`FsumE'/`Yavg'')
	}
end


cap program drop outFdisaglincomEND
program def outFdisaglincomEND
	* syntax anything
	cap qui lincom $annaggsum
	local FsumA=r(estimate)
	local pvalFsumA=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $peraggsum
	local FsumP=r(estimate)
	local pvalFsumP=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $extaggsum
	local FsumE=r(estimate)
	local pvalFsumE=2*ttail($addClust,abs(r(estimate)/r(se)))
	matrix pvalANNUAL_END$est = (nullmat(pvalANNUAL_END$est ) \ `pvalFsumA')
	matrix pvalPERENNIAL_END$est = (nullmat(pvalPERENNIAL_END$est ) \ `pvalFsumP')
	matrix pvalEXTRACT_END$est = (nullmat(pvalEXTRACT_END$est ) \ `pvalFsumE')
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	if "$est"=="_LOG" {
		matrix impactANNUAL_END$est = (nullmat(impactANNUAL_END$est ) \ `FsumA')
		matrix impactPERENNIAL_END$est = (nullmat(impactPERENNIAL_END$est ) \ `FsumP')
		matrix impactEXTRACT_END$est = (nullmat(impactEXTRACT_END$est ) \ `FsumE')
	}
	else {
		matrix impactANNUAL_END$est = (nullmat(impactANNUAL_END$est ) \ `=`FsumA'/`Yavg'')
		matrix impactPERENNIAL_END$est = (nullmat(impactPERENNIAL_END$est ) \ `=`FsumP'/`Yavg'')
		matrix impactEXTRACT_END$est = (nullmat(impactEXTRACT_END$est ) \ `=`FsumE'/`Yavg'')
	}
end


cap program drop outFdisaglincom
program def outFdisaglincom
	* syntax anything
	cap qui lincom $annaggsum
	local FsumA=r(estimate)
	local pvalFsumA=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $peraggsum
	local FsumP=r(estimate)
	local pvalFsumP=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $extaggsum
	local FsumE=r(estimate)
	local pvalFsumE=2*ttail($addClust,abs(r(estimate)/r(se)))
	matrix pvalANNUAL = (nullmat(pvalANNUAL) \ `pvalFsumA')
	matrix pvalPERENNIAL = (nullmat(pvalPERENNIAL) \ `pvalFsumP')
	matrix pvalEXTRACT = (nullmat(pvalEXTRACT) \ `pvalFsumE')
	cap qui test $annagg
	local FjointA=r(p)
	cap qui test $peragg
	local FjointP=r(p)
	cap qui test $extagg
	local FjointE=r(p)
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	if (`pvalFsumA'>=.) {
		outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC addstat( Mean Dep Var, `Yavg') append
	}
	else{
		outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC addstat(Apct imp, `=`FsumA'/`Yavg'', +A, `FsumA', p, `pvalFsumA', Ppct imp, `=`FsumP'/`Yavg'', +P, `FsumP', p, `pvalFsumP', Epct imp, `=`FsumE'/`Yavg'', +E, `FsumE', p, `pvalFsumE', avgY, `Yavg') append
	}
end


cap program drop outFdisaglincomIMPORT
	program def outFdisaglincomIMPORT
	* syntax anything
	cap qui lincom $annaggsum
	local FsumA=r(estimate)
	local pvalFsumA=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $peraggsum
	local FsumP=r(estimate)
	local pvalFsumP=2*ttail($addClust,abs(r(estimate)/r(se)))
	cap qui lincom $extaggsum
	local FsumE=r(estimate)
	local pvalFsumE=2*ttail($addClust,abs(r(estimate)/r(se)))
	matrix pvalANNUAL = (nullmat(pvalANNUAL) \ `pvalFsumA')
	matrix pvalPERENNIAL = (nullmat(pvalPERENNIAL) \ `pvalFsumP')
	matrix pvalEXTRACT = (nullmat(pvalEXTRACT) \ `pvalFsumE')
	cap qui test $annagg
	local FjointA=r(p)
	cap qui test $peragg
	local FjointP=r(p)
	cap qui test $extagg
	local FjointE=r(p)
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	qui testparm $IMPX
	local pvalIMP=r(p)
	if (`pvalFsumA'>=.) {
		outreg2 using $table, asterisk(se) drop($year $country_year It* Iccid*)  excel $fmtDEC addstat( Mean Dep Var, `Yavg') append
	}
	else {
		outreg2 using $table, asterisk(se) drop($year $country_year It* Iccid*)  excel $fmtDEC addstat(Apct imp, `=`FsumA'/`Yavg'', +A, `FsumA', p, `pvalFsumA', Ppct imp, `=`FsumP'/`Yavg'', +P, `FsumP', p, `pvalFsumP', Epct imp, `=`FsumE'/`Yavg'', +E, `FsumE', p, `pvalFsumE', avgY, `Yavg', F test import pval, `pvalIMP') append
	}
end

cap program drop outFdisaglincom_IV
program def outFdisaglincom_IV
	* syntax anything
	qui su `e(depvar)' if e(sample)
	local Yavg=r(mean)
	local Ffirst = e(widstat)
	local IDp = e(idp)
	
	* This adds these stats to outreg only if they are produced to avoid problems due to the covariance matrix not having full rank
	local Ffirst_string ""
	if "`Ffirst'" != "." {
		local Ffirst_string ", First Stage F Stat, `Ffirst'"
	}
	local IDp_string "" 
	if "`IDp'" != "." {
		local IDp_string ", Underidentification test pval, `IDp'"
	}
	
	* Notice that this has optionally added statistics (see above)
	outreg2 using $table, asterisk(se) $addClust drop($year $country_year It* Iccid*)  excel $fmtDEC addstat( Mean Dep Var, `Yavg' `Ffirst_string' `IDp_string') append
end

